package com.quick.controller.system;

import java.util.List;

import com.quick.common.core.domain.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.quick.common.annotation.Log;
import com.quick.common.core.controller.BaseController;
import com.quick.common.core.domain.ApiResponse;
import com.quick.common.enums.BusinessTypeEnum;
import com.quick.system.domain.SysDictData;
import com.quick.system.service.SysDictDataService;
import com.quick.common.utils.poi.ExcelUtil;

/**
 * 字典数据Controller
 *
 * @Author: 吃素不吃肉
 * @Date: Tue Jul 25 14:14:44 CST 2023
 */
@Api(value = "字典数据管理", tags = {"字典数据操作接口"})
@RestController
@RequestMapping("/system/dictData")
public class SysDictDataController extends BaseController {
    @Autowired
    private SysDictDataService sysDictDataService;

    /**
     * 查询字典数据列表
     */
    @PreAuthorize("@ps.hasPermission('system:dict:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页查询字典数据列表")
    public ApiResponse list(SysDictData sysDictData) {
        try {
            startPage();
            List<SysDictData> list = sysDictDataService.selectSysDictDataList(sysDictData);
            return paging(list);
        } finally {
            clearPage();
        }

    }

    /**
     * 导出字典数据列表
     */
    @PreAuthorize("@ps.hasPermission('system:dict:export')")
    @Log(title = "字典数据", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出字典数据列表")
    public void export(HttpServletResponse response, SysDictData sysDictData) {
        List<SysDictData> list = sysDictDataService.selectSysDictDataList(sysDictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<>(SysDictData.class);
        util.exportExcel(response, list, "字典数据数据");
    }

    /**
     * 获取字典数据详细信息
     */
    @PreAuthorize("@ps.hasPermission('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    @ApiOperation(value = "获取字典数据详细信息")
    public ApiResponse getInfo(@PathVariable("dictCode") Long dictCode) {
        return success(sysDictDataService.selectSysDictDataByDictCode(dictCode));
    }

    /**
     * 新增字典数据
     */
    @PreAuthorize("@ps.hasPermission('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessTypeEnum.INSERT)
    @PostMapping
    @ApiOperation(value = "新增字典数据")
    public ApiResponse add(@RequestBody SysDictData sysDictData) {
        return success(sysDictDataService.insertSysDictData(sysDictData));
    }

    /**
     * 修改字典数据
     */
    @PreAuthorize("@ps.hasPermission('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessTypeEnum.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改字典数据")
    public ApiResponse edit(@RequestBody SysDictData sysDictData) {
        return success(sysDictDataService.updateSysDictData(sysDictData));
    }

    /**
     * 删除字典数据
     */
    @PreAuthorize("@ps.hasPermission('system:dict:remove')")
    @Log(title = "字典数据", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{dictCodes}")
    @ApiOperation(value = "删除字典数据")
    public ApiResponse remove(@PathVariable Long[] dictCodes) {
        return success(sysDictDataService.deleteSysDictDataByDictCodes(dictCodes));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @PreAuthorize("@ps.hasPermission('system:dict:list')")
    @GetMapping(value = "/type/{dictType}")
    @ApiOperation(value = "根据字典类型查询字典数据信息")
    public ApiResponse dictType(@PathVariable String dictType) {
        List<Dict> data = sysDictDataService.selectDictDataByType(dictType);
        return success(data);
    }


}
